I have created a post format of video and also created a single-video.php to display a single video post.
added following code to functions.php include single video page template.
function templateForSingleVideo( $template ) {
if ( is_single() && has_post_format() ) {
$post_format_template = locate_template( 'single-' . get_post_format() . '.php' );
if ( $post_format_template ) {
$template = $post_format_template;
}
}
return $template;
}
add_filter( 'template_include', 'templateForSingleVideo' );
it works perfectly fine. but now I want a page where the posts (post format=video) will be shown from all the categories. Just want to show all the videos..
Sorry for my bad English.
Thanks
Related
I'm not sure what i'm doing wrong, been researching for the past few days but hopeless.
WordPress Settings: Static Page: Home
GOAL: to replace the query so it takes ALL front page data including the following:
front page post content
front page title
front page metadata
and to display the content, title, metadata in a natural way by calling in theme page template
// Takes front page title
<?php wp_title(); ?>
// Takes front page config
<?php wp_head(); ?>
// takes front page post and display content
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo the_content(); ?>
<?php endwhile; endif; ?>
Target URL: homepage.com/amp (not existing page)
but defined as shown below
// defines AMP variable
define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );
// enable URL endpoint rewrite for /amp
add_rewrite_endpoint( AMP_QUERY_VAR, EP_ALL );
MAIN PROBLEM in the Current Code Below
function front_page_post_AMP( $query ) {
// Get's the Current Browser URL
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
// Homepage AMP URL
$front_page_amp_url = get_site_url() . "/amp";
// check if the current browser URL is homepage.com/amp
if ( strcasecmp( $current_url, $front_page_amp_url ) == 0 && $query->is_main_query() )
{
// gets front page id
$front_page_id = get_option( 'page_on_front' );
// replace query id
$query->set( 'page_id', $front_page_id );
return $query;
}
}
add_action( 'pre_get_posts', 'front_page_post_AMP' );
Current Results:
Page Title = not found
Page post content = empty/null
Off course it will return 404 not found because amp page or amp post is not exist.
My idea is there is an alternative way it can return to a valid url by add custom endpoint for your website
reference : https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint
add this following code to your theme function
add_rewrite_endpoint( 'amp', EP_ALL );
then save permalink , then try acces any url of your website followed by /amp
example : homepage.com/amp , homepage.com/single-post/amp
Hope it helps
spent days studying how WP_Query and pre_get_posts works and finally got the answer
/*
* if user is accessing the not existing page homepage.com/amp/
* set the query to get the frontpage post
*/
function front_page_post_AMP( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() ) {
return;
}
/*
* check if endpoint is AMP
* check if the array count is 1 (means no parameters, and most likely will result in 404)
*/
if ( is_amp_endpoint() &&
count( $query->query ) == 1 &&
! isset( $query->query['page'] ) )
{
// Get's the Current Browser URL
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
// Homepage AMP URL
$front_page_amp_url = get_site_url() . "/amp";
/*
* If current URL is homepage.com/amp
* set the empty query to get the front page post
*/
if ( strcasecmp( $current_url, $front_page_amp_url ) == 0 ) {
$front_page_id = get_option( 'page_on_front' );
$query->set( 'page_id', $front_page_id );
$query->set( 'post_type', "page" );
}
}
return;
}
add_action( 'pre_get_posts', 'front_page_post_AMP' );
With this solution, you can use the natural way of calling the title and content on your page template as shown below
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo the_content(); ?>
<?php endwhile; endif; ?>
I'm struggling with one thing. I've got such wordpress function:
function wpq_insert_attachment_data($data, $postarr){
if (!is_single() ) {
$posttitle = get_the_title( $postarr['post_parent'] );
$data['post_title'] = $posttitle;
$data['post_name'] = $posttitle;
return $data;
}}
add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
It works superb but it covers all single/custom post type/pages etc. Is there any way to EXCLUDE pages from that function? I've tried sorting it out with is_single() yet without success.
Use is_singular in your statement to target specific post types.
Can also do an array to include or exclude.
If (! is_singular()) ..... or.....
(! is_singular(array('page','food',foo')))
Then it will only run on the singles for whichever post type you're targeting.
Looks like you just need to tweak your conditional statement:
if (!is_single() ) {
Should become:
if (!is_page() ) {
Load the function only on specific page, add your page id in is_page(id_here) :
function wpq_insert_attachment_data($data, $postarr){
if ( is_page(page_id)){
$posttitle = get_the_title( $postarr['post_parent'] );
$data['post_title'] = $posttitle;
$data['post_name'] = $posttitle;
return $data;
}
}
add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
you can also add your page slug instead of id like :
if ( is_page('slug'))
Or exclude page(s)
if ( !is_page('slug'))
if ( !is_page(array('slug-1', 'slug-2') )
How do you add a category template via a Wordpress plugin. It needs to be for a specific category. So if the category is 'category-awesome' load 'category-awesome.php'. I've come across using the single template hook e.g:
add_filter('single_template', 'my_custom_template');
But haven't found anything for categories or a single category.
Many Thanks
Think I solved it. Use the template include filter hook:
function category_awesome( $template ) {
if (is_category( 'awesome' )) {
$new_template = dirname(__FILE__) . '/custom-template-awesome.php';
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
add_filter( 'template_include', 'category_awesome' );
I'm new on wordpress and i'm trying to create an archive page for multiple custom post type.
I have two custom post type, 'fair' and 'exhibition'
I currently have two archive page, with pre get post action :
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // VERY important, targets only front end queries
&& $q->is_main_query() // VERY important, targets only main query
&& $q->is_post_type_archive( 'fair' ) // Which post type archive page to target
) {
$q->set( 'oderby', 'meta_value_num' );
$q->set( 'meta_key', '_datepicker');
$q->set('showposts', 3);
$q->set('is_post_type_archive', false);
$q->set('order', 'ASC');
// Rest of your arguments to set
}
});
both of the custom post type have the same meta box and i want to display both post type on the archive page "fair" or in a new one if necessary
i tried to add
$q->set('post_type', array('fair', 'exhibition'));
to the function.
When i do that i have my both post type but my custom archive page ("archive-fair.php") do not seems to be called
How can i perform the archive page for both post type properly with functional pagination ?
Thanks and sorry for my english, i hope it's understandable.
Edit : my archive-post.php page look like :
<?php get_header();
while ( have_posts() )
{
the_post();
$p_type = get_post_type(get_the_ID());
if ($p_type == 'fair')
$img = get_post_meta(get_the_ID(), "wp_fair_attachment", true);
else
$img = get_post_meta(get_the_ID(), "wp_exhibition_attachment", true);
?>
some html
<?php } ?>
An alternative approach is to use template_include
Try this code and let me know if it works:
add_filter( 'template_include', function( $template ){
$your_types = array( 'fair', 'exhibition' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $your_types ) )
return $template;
return get_stylesheet_directory() . '/single-fair.php';
});
I have wordpress flash game website. Into the admin panel, where I add or edit post I have field named Swf URL: where I add SWF link and it links to the post where it shows flash game. I want to do same but not in the post, I want to use this function (adding game with the help of swf) to home page.
How it is now:
How I want to see:
functions code:
load_theme_textdomain( "freebabyhazelgames", TEMPLATEPATH."/languages" );
$locale = get_locale( );
$locale_file = TEMPLATEPATH."/languages/{$locale}.php";
if ( is_readable( $locale_file ) )
{
require_once( $locale_file );
}
$sp_boxes = array( "Game Details" => array( array( "thumb", "Game image URL:" ), array( "game", "Swf URL:" ) ) );
add_action( "admin_menu", "sp_add_custom_box" );
add_action( "save_post", "sp_save_postdata", 1, 2 );
if ( !function_exists( "get_custom_field" ) )
{
function get_custom_field( $field )
{
global $post;
$custom_field = get_post_meta( $post->ID, $field, true );
echo $custom_field;
}
}
post code:
<div id="playgame">
<script type="text/javascript">
<!--
swf("<?php $values = get_post_custom_values("game"); echo $values[0]; ?>", "701", "550");//-->
</script>
</div>
By the way I tried to change dirrectory or to copy single.php to index.php but both not worked.
If I understand you right, you want the game to appear on the main page without entering a post? In that case, I think you can try using a static frontpage instead of latest posts.
You can find it on Appearance -> Customize, and then choose the frontpage to be static. That way you can have your game on the static frontpage.