On my WordPress website, I have an archive page e.g.: http://www.example.com/2017. My archive.php file has a query to show posts as well as two custom post types: case-studies and media:
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => array('post','case-studies','media'), 'posts_per_page' => 3 ) );
query_posts( $args );
while ( have_posts() ) : the_post();
The archive page will only show provided there are posts under post (the standard post type that is included within WordPress) otherwise it will 404, even though there are posts under case-studies and media.
Is there a workaround this?
Here is a link to my archive.php for those who are interested: archive.php
I've had this problem before. You can include a function in your functions.php file which can redirect the page to archive.php instead of the usual 404. See if this works:
function wpd_date_404_template( $template = '' ){
global $wp_query;
if( isset($wp_query->query['year'])
|| isset($wp_query->query['monthnum'])
|| isset($wp_query->query['day']) ){
$template = locate_template( 'archive.php', false );
}
return $template;
}
add_filter( '404_template', 'wpd_date_404_template' );
Credit to Milo at the WordPress StackExchange. See his answer here: Preventing 404 error on empty date archive
function wpd_date_404_template( $template = '' ){
global $wp_query;
if(
isset($wp_query->query['year']) ||
isset($wp_query->query['monthnum']) ||
isset($wp_query->query['day'])
) {
$template = locate_template( 'archive.php', false );
if ( isset( $wp_query->query['post_type'] ) ) {
$located = locate_template( 'archive-' . $wp_query->query['post_type'] . '.php', false );
$template = $located !== '' ? $located : locate_template( 'archive.php', false );
}
}
return $template;
}
add_filter( '404_template', 'wpd_date_404_template' );
Just used #Dev1997 his answer to solve the issue. With a small improvement to use the specific template for the current (custom) post_type. Maybe it will help someone who is using custom post types.
Related
I'm trying to make a download script for a password protected wordpress site. To make use of PHPs readfile() function I need to retrieve the full attachment URL based on it's ID i am passing to the download script.
I made a Custom Post Type named Downloads and also changed it's upload directory to a folder inside wp-content also named downloads.
Here is the code for it:
add_filter( 'upload_dir', 'custom_upload_directory' );
function custom_upload_directory( $args ) {
$id = $_REQUEST['post_id'];
$parent = get_post( $id )->post_parent;
if( "downloads" == get_post_type( $id ) || "downloads" == get_post_type( $parent ) ) {
$args['path'] = WP_CONTENT_DIR . '/downloads';
$args['url'] = WP_CONTENT_URL . '/downloads';
}
return $args;
}
Upload works fine and when I click the link to the desired file, the ID is passed to a script via $_POST, which also works fine. But I just can't figure out a way to get the right file URL. Here's what I tried:
wp_get_attachment_url( $id ); // returns: example.com/wp-content/uploads/html/theme/wp-content/downloads/filename.ext
wp_get_attachment_link( $id ); // returns: slug
get_attachment_link( $id ); // returns: example.com/downloads/file (without .ext)
get_attached_file( $id, true ); // returns: html/theme/wp-content/downloads/filename.ext
get_post_meta( $id, '_wp_attached_file', false ); // returns: html/theme/wp-content/downloads/filename.ext
wp_get_attachment_metadata( $id ); // returns nothing
What I expected any of those functions to return was example.com/wp-content/downloads/filename.ext
But as you can see, some mix up the default upload directory and combine it with the new one while others just return half of the full URL (html/theme/... it's the directory the website sits on the server). So any ideas would be appreciated.
Hours, days and even weeks later I finally found an answer and modified it to fit my needs. I came as far as displaying the right URL (the modified one) inside the file upload lightbox of Wordpress. But after publishing/updating the post it went back to the same old .../wp-content/uploads/file.ext URL.
Someone else, somewhere else got exactly the same problem and fortunately it is said there, that you must not just alter $args['path'] and $args['url'] but you also have to alter basedir, baseurl and subdir.
So, the complete code to change a custom post types upload directory (in this case I chose the directory .../wp-content/downloads) is the following:
add_filter( 'upload_dir', 'change_upload_dir' );
function change_upload_dir( $args ) {
$id = $_REQUEST['post_id'];
if( get_post_type( $id ) == 'downloads' ) {
$args['basedir'] = wp_normalize_path( WP_CONTENT_DIR . 'downloads' );
$args['baseurl'] = content_url() . '/downloads';
$args['path'] = $args['basedir'];
$args['url'] = $args['baseurl'];
$args['subdir'] = '';
return $args;
}
}
So now, calling wp_get_attachment_url() finally results in something like example.com/wp-content/downloads/file.ext
I would like to remove the ‘Category’ from header title text on category pages in wordpress. Have tried several plugins that claim to remove these but don’t seem to work with my version.. (4.9.6)
Here a screenshot of how it looks now:
https://imgur.com/a/E2gdr2l
So would want to have only ‘News’ displayed there. Anyone knows a way of getting rid of the Category before it?
All the best and thanks in advance!
In Magazine Base theme:
The title is added from the file inc/hooks/header-inner-page.php using the function the_archive_title() like
the_archive_title('<h1 class="entry-title">', '</h1>');
to remove Category from the output you need to add the following filter in your functions.php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
}
return $title;
});
Code credit: Here
That would fix the issue.
Just add a line into your functions.php (After WP5.5).
add_filter( 'get_the_archive_title_prefix', '__return_false' );
check in archive.php or category.php
it is loading from here
<h1 class="entry-title">Category: News</h1>
you can check and edit any of the above mentioned files.
I want to generate a placeholder-text in the 'content editor' if post has post format 'gallery'. But I can't get it working:
functions.php:
add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
if ( 'post' == $post->post_type && has_post_format('gallery')) {
$content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project's page. Please use the fields above for text.</i>';
return $content;
}
}
This should do it:
add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
$format = get_post_format($post->ID);
if ( 'post' == $post->post_type && $format == 'gallery') {
$content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project's page. Please use the fields above for text.</i>';
}
return $content;
}
Also, please make sure that your theme is set to actually have post-format support with add_theme_support and that your post has the correct post format (gallery).
I am supporting a website running on Wordpress 3.8 with a wpzoom theme (zenko) on top of that with most of the articles posts running on multiple pages.
For pagination navigation we use the WP-PageNavi plugin http://wordpress.org/extend/plugins/wp-pagenavi/ with the function
<?php wp_pagenavi( array( 'type' => 'multipart' ) ); ?>
inserted in the single.php code after the
<?php the_content(); ?> function.
Recently we added the WordPress Related Posts http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/ . However when activated the related posts appear before the page navigation links- buttons and I would prefer to change it to appear after them.
Looking around in the related post plugin's code I found the corresponding formating code as follows:
global $wp_rp_output;
$wp_rp_output = array();
function wp_rp_add_related_posts_hook($content) {
global $wp_rp_output, $post;
$options = wp_rp_get_options();
if ($content != "" && $post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
if (!isset($wp_rp_output[$post->ID])) {
$wp_rp_output[$post->ID] = wp_rp_get_related_posts();
}
$content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
$content = $content . $wp_rp_output[$post->ID];
}
return $content;
}
add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
My question is how can I modify the code so that the array of related post appear after the page navigation links
I have a WordPress Admin page where you can add a "listing" within this Admin Page I have added many fields which transfer variables over to the listing page which is created/updated. Within this admin page I am trying to take the string after the last "&" in the url which is manually enter in the url before loading the page
see code below:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$id = substr( $url, strrpos( $url, '&' )+1 );
NOW the code below here is called when the ABOVE admin page is submitted to update the "listing" what I need to do is somehow get the variable $id to pass through to this function and be used again within the submit code. Both of these functions are within functions.php so somehow it should be easy to do this!
This may not be "Wordpress Specific" but is a Wordpress Specific situation where I had this code working on a non-wordpress site. Please help!
$dirname = "../wp-content/themes/Explorable/".$id."/";
$images = glob($dirname."*.jpg");
// Open a known directory, and proceed to read its contents
foreach($images as $image) {
$imageNameLong = substr($image, -14);
$imageName = substr($imageNameLong,0 , -4);
if ( isset( $_POST[$imageName.'links'] ) )
update_post_meta( $post_id, '_'.$imageName.'links', sanitize_text_field( $_POST[$imageName.'links'] ) );
else
delete_post_meta( $post_id, '_'.$imageName.'links' );
if ( isset( $_POST[$imageName.'heading'] ) )
update_post_meta( $post_id, '_'.$imageName.'heading', sanitize_text_field( $_POST[$imageName.'heading'] ) );
else
delete_post_meta( $post_id, '_'.$imageName.'heading' );
};