Wordpress에서 임의의 투고 가져오기
워드프레스에서 임의의 투고를 받으려면 어떻게 해야 하나요?
블로그에서 임의의 투고로 이동하는 버튼을 페이지에 표시하고 싶습니다.페이지에 무작위로 게시물을 표시하는 것이 아니라 해당 게시물로 연결되는 링크를 원합니다.구글과 stackoverflow에서 코드를 검색하려고 했지만 실패했습니다.
감사합니다.
갱신:
템플릿 코드는 다음과 같습니다.
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
페이지 템플릿을 만들고 다음 코드를 사용하여 임의의 투고를 가져옵니다.
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
페이지에서 다음을 사용합니다.
<a href="the link to the page">see a random post</a>
원하는 결과를 얻을 수 있는 게시물을 찾았습니다.
다음은 wpbeginner 블로그 투고에서 복사/붙여진 솔루션입니다.저작권 침해를 의도한 것은 아닙니다.
다음 코드를 에 추가합니다.functions.php파일:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
사용하다mydomain.com/random/당신의 것으로서href임의의 포스트로 연결되는 버튼을 클릭합니다.
도와주신 모든 분들께 감사드립니다.
건배!
사이드바나 메뉴에서 링크로 사용할 수 있는 임의의 투고로 리다이렉트하는 URL이 더 편리하다고 생각합니다.단일 WP 사이트이고 wp.com에서도 블로그는 매우 간단합니다.
http://mygroovywpsite.me/
랜덤으로 추가하기만 하면 됩니다.
http://mygroovywpsite.me/?random
멀티사이트 설치의 서브사이트에서는 이 코드(위의 wp_beginner 코드도)가 동작하지 않는 것을 알았습니다.어느 접근법도 홈 페이지를 로드했을 뿐입니다.아마 뭔가 이상한 캐시 문제가 있었나봐요많은 사이트에서는 플러그 인을 사용하지 않고 몇 가지 단계를 수행합니다.
우선, 「랜덤」이라고 하는 이름의 페이지를 사이트내에서 작성한다./슬래그 「랜덤」이 들어간 페이지는, 컨텐츠가 필요 없습니다.
그런 다음 페이지 랜덤을 만듭니다.php 템플릿
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
그러면 블로그 링크에 대한 리다이렉트를 얻을 수 있습니다./random ww/o.htaccess와 씨름하는 경우
이 방법으로 쿼리를 수정해야 했기 때문에 커스텀 투고 타입에 따라서는 카테고리로 한정할 수도 있습니다.
ORDER BY RAND()를 사용하여 mySQL 쿼리 사용을 억제했기 때문에 문제가 된 사이트는 하나뿐이었습니다.
랜덤 투고를 표시하는 또 다른 간단한 솔루션
1. 먼저 커스텀 페이지 템플릿을 만듭니다.랜덤 투고 또는 원하는 이름으로 지정하세요!
2. 페이지를 열고 기본 wp 루프를 제거하고 아래 코드를 붙여넣습니다.
3. 투고 번호를 변경하려면 숫자 '1'을 원하는 대로 변경하세요!
<?php query_posts(array('orderby' => 'rand', 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?> <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1> <?php the_content(); ?> <?php endwhile; endif; ?>
출처 : http://www.yengkokpam.com/displays-random-posts-in-a-page/
체크 내용
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
언급URL : https://stackoverflow.com/questions/8672401/get-random-post-in-wordpress
'programing' 카테고리의 다른 글
| 문서 기반 데이터베이스와 키/가치 기반 데이터베이스의 차이점 (0) | 2023.03.26 |
|---|---|
| Wordpress에서 실제로 준비된 문장을 얻으려면 어떻게 해야 합니까? (0) | 2023.03.26 |
| MongoDB 데이터베이스 연결 열기 (0) | 2023.03.26 |
| 새 탭에서 React-Router 링크 열기 (0) | 2023.03.26 |
| Oracle에서 문자열 연결 연산자는 무엇입니까? (0) | 2023.03.26 |