調べてもなかなか検索できないので教えてください。
ウィジェットについて、すごくご丁寧に教えていただいたのですが、"http://投稿一覧のURL"の部分で、記事一覧の投稿が表示されるページが複数あったときに、その記事の載っている記事一覧のページにページ遷移させたいのですが、やりかたが分かりませんので教えてください。調べ方が下手でなかなかそのやり方が書いてあるサイトにたどり着けなく、6時間はまっています。
index.php
php
1<?php get_header(); ?> 2 <div id="main" class="container"> 3 <div id="posts"> 4 5 <?php if(have_posts()): 6 while(have_posts()): 7 the_post(); 8 9 ?> 10 <?php $p_slug = get_page_uri(get_the_ID()); ?> 11 <div id="<?php echo $p_slug ?>"></div> 12 13 <div id="test"></div> 14 <div class="post"> 15 <div class="post-header"> 16 <h2> 17 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 18 </h2> 19 </div> 20 <div class="post-meta"> 21 <?php echo get_the_date(); ?> 【<?php the_category(', ') ?>】 22 </div> 23 <div class="post-content"> 24 <div class="post-image"> 25 26 <?php if(has_post_thumbnail()): ?> 27 <?php the_post_thumbnail(array(100, 100)); ?> 28 <?php else: ?> 29 <img src="<?php echo get_template_directory_uri(); ?>/images.jpg" width="100" height="100"> 30 <?php endif; ?> 31 32 </div> 33 <div class="post-body"> 34 <div class="post-body-excerpt"> 35 <?php the_content('',false,''); ?> 36 </div> 37 38 <div class="more"><a href="<?php the_permalink(); ?>">続きを読む</a></div> 39 <a href="<?php echo '#'.$p_slug ?>">ここ</a> 40 </div> 41 42 </div> 43 </div> 44 <?php endwhile; 45 else: 46 ?> 47 48 <p>記事はありません!</p> 49 50 <?php endif; 51 ?> 52 53 <div class="navigation"> 54 <div class="prev"><?php previous_posts_link(); ?></div> 55 <div class="next"><?php next_posts_link(); ?></div> 56 </div> 57 </div><!--/posts--> 58 <?php get_sidebar(); ?> 59 </div><!--/main --> 60 <?php get_footer(); ?>
single.php
php
1<?php get_header(); ?> 2 <div id="main" class="container"> 3 <div id="posts"> 4 5 <?php if(have_posts()): 6 while(have_posts()): 7 the_post(); 8 9 ?> 10 <div class="post"> 11 <div class="post-header"> 12 <h2> 13 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 14 </h2> 15 </div> 16 <div class="post-meta"> 17 <?php echo get_the_date(); ?> 【<?php the_category(', ') ?>】 18 </div> 19 <div class="post-content"> 20 <p> 21 <?php the_content(); ?> 22 </p> 23 24 </div> 25 </div> 26 27 <div class="navigation"> 28 <div class="prev"><?php previous_post_link(); ?></div> 29 <div class="next"><?php next_post_link(); ?></div> 30 </div> 31 32 <?php endwhile; 33 else: 34 ?> 35 36 <p>記事はありません!</p> 37 38 <?php endif; 39 ?> 40 41 </div><!--/posts--> 42 <?php get_sidebar(); ?> 43 </div><!--/main --> 44 <?php get_footer(); ?>
sidebar.php
php
1<div id="sidebar"> 2 <?php dynamic_sidebar(); ?> 3 </div><!--/sidebar-->
function.php
php
1<?php 2 3add_theme_support( 'custom-header' ); 4 5add_theme_support('menus'); 6 7register_sidebar( 8 array( 9 before_widget => '<div class="widget">', 10 after_widget => '</div>', 11 before_title => '<h3>', 12 after_title => '</h3>', 13 ) 14); 15 16add_theme_support('post-thumbnails'); 17 18$cssdir = get_stylesheet_directory_uri(); 19wp_enqueue_script( 'theme-script', $cssdir.'/script.php', array('jquery')); 20 21// ウィジェットでショートコードを使えるようにする 22add_filter( 'widget_text', 'do_shortcode' ); 23 24// 任意のPHPファイルをショートコード化して扱えるようにする 25function php_shortcode( $params = array() ) { 26 extract( shortcode_atts( array( 27 'file' => 'default' 28 ), $params )); 29 ob_start(); 30 include( get_template_directory() . "/$file.php" ); 31 return ob_get_clean(); 32} 33add_shortcode( 'my_php', 'php_shortcode' ); 34 35?>
recent_posts.php
php
1<?php 2$args = array( 3 'post_type' => 'post', //ポストタイプ 4 'posts_per_page' => 5, //表示件数 5 ); 6 $the_query = new WP_Query( $args ); 7 if ( $the_query->have_posts() ): 8 while ( $the_query->have_posts() ): 9 $the_query->the_post(); 10?> 11<p><a href="http://投稿一覧のURL" ><?php the_title();?></a></p> 12<?php 13endwhile; 14endif; 15wp_reset_query(); 16?>
カスタムHTMLのウィジェットを追加して以下を書き込む
[my_php file="recent_posts"]
ワードプレスのjavascriptのプラグイン
javascript
1jQuery(function() { 2 jQuery(".menu li").hover(function() { 3 jQuery(this).children('ul').show(); 4 }, function() { 5 jQuery(this).children('ul').hide(); 6 });//プルダウンメニューが表示されます 7 8 var page; 9 10 jQuery('.more a').on('click', function(event) { 11 //aリンクの動作を停止 12 event.preventDefault(); 13 //リンク先URLを取得 14 page = jQuery(this).attr('href'); 15 jQuery(this).parent().load(page+' div.post-content p',function(){ 16 jQuery(this).parent().children().eq(0).hide(); 17 }); 18 //続きを読む ajaxでページ遷移せずにsingle.phpの一部が表示される 19 }); 20 jQuery('a[href^=#]').click(function(){ 21 var speed = 500; 22 var href= jQuery(this).attr("href"); 23 var target = jQuery(href == "#" || href == "" ? 'html' : href); 24 var position = target.offset().top; 25 jQuery("html, body").animate({scrollTop:position}, speed, "swing"); 26 return false; 27 });//ページ内リンクすることができる 28});
関係のないテンプレートの表記を削除しました。
昨日教えていただいたコードでrecent_posts.phpの"http://投稿一覧のURL"において、http://localhost/wordpress/blog/とすると、そのページにしか遷移しないので、それをlocalhost/wordpress/blog/2/やlocalhost/wordpress/blog/3/といった風にphpのコードを埋めてページ遷移させたいです。よろしくお願いします。
recent_posts.php
php
1<?php 2$args = array( 3 'post_type' => 'post', 4 'posts_per_page' => 5, 5); 6$the_query = new WP_Query( $args ); 7if ( $the_query->have_posts() ): 8while ( $the_query->have_posts() ): 9$the_query->the_post(); 10$category = get_the_category(); 11$category_link = get_category_link( $category[0]->term_id ); 12?> 13<?php $p_slug = get_page_uri(get_the_ID()); ?> 14<p><a href="<?php echo $category_link.'#'.$p_slug; ?>" ><?php the_title(); ?></a></p> 15<?php 16endwhile; 17endif; 18wp_reset_query();
とやるとリンクさきのidにページ内リンクできました。
回答2件
あなたの回答
tips
プレビュー